package com.cloud.cloudshop.shop.controller;

import com.cloud.cloudshop.shop.service.GoodsCatesService;
import com.cloud.cloudshop.util.PageUtils;
import com.cloud.common.entity.GoodsCatesEntity;
import com.cloud.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author luoJie
 * @email luoJie@gmail.com
 * @date 2020-10-04 16:40:24
 */
@Slf4j
@RestController
@RequestMapping("shop/goodscates")
public class GoodsCatesController {
    @Autowired
    private GoodsCatesService goodsCatesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = goodsCatesService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @Cacheable(value = {"cates"},key = "#root.methodName")
    @RequestMapping("/tree")
    public R list() {
        List<GoodsCatesEntity> goodsCatesEntities = goodsCatesService.listTree();

        List<GoodsCatesEntity> allFirstLevel = goodsCatesEntities.stream().filter(goodsCatesEntity -> "0".equals(goodsCatesEntity.getPid()))
                .map(goodsCatesEntity -> {
                    goodsCatesEntity.setChildren(getChildren(goodsCatesEntity, goodsCatesEntities));
                    return goodsCatesEntity;
                })
                .sorted(Comparator.comparingInt(GoodsCatesEntity::getCateOrder))
                .collect(Collectors.toList());

        return R.ok().put("data", allFirstLevel);
    }

    public List<GoodsCatesEntity> getChildren(GoodsCatesEntity root, List<GoodsCatesEntity> allEntities) {

        List<GoodsCatesEntity> collect = allEntities.stream().filter(goodsCatesEntity -> root.getId().equals(goodsCatesEntity.getPid()))
                .map(goodsCatesEntity -> {
                    goodsCatesEntity.setChildren(getChildren(goodsCatesEntity, allEntities));
                    return goodsCatesEntity;
                })
                .sorted(Comparator.comparingInt(GoodsCatesEntity::getCateOrder))
                .collect(Collectors.toList());

        return collect;

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        GoodsCatesEntity goodsCates = goodsCatesService.getById(id);

        return R.ok().put("goodsCates", goodsCates);
    }

    /**
     * 保存
     */
    @CacheEvict(value = "cates",allEntries = true)
    @RequestMapping("/save")
    public R save(@RequestBody @Valid GoodsCatesEntity goodsCates) {
        goodsCatesService.save(goodsCates);

        return R.ok();
    }

    /**
     * 修改
     */
    @CacheEvict(value = "cates",allEntries = true)
    @RequestMapping("/update")
    public R update(@RequestBody GoodsCatesEntity goodsCates) {
        goodsCatesService.updateById(goodsCates);

        return R.ok();
    }

    /**
     * 删除
     */
    @CacheEvict(value = "cates",allEntries = true)
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        goodsCatesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
